package com.caikun.space_serve.controller;

import com.caikun.model.common.Result;
import com.caikun.model.dto.SpaceStatusDTO;
import com.caikun.model.entity.Space;
import com.caikun.model.vo.PlaceTypeVo;
import com.caikun.space_serve.service.SpaceService;
import com.github.pagehelper.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.http.HttpRequest;
import java.util.Enumeration;
import java.util.List;


/**
 * 洗车位表
 * (Space)表控制层
 *
 * @author makejava
 * @since 2025-07-16 16:22:34
 */
@Slf4j
@RestController
@RequestMapping("/place")
public class SpaceController {
    /**
     * 服务对象
     */
    @Autowired
    private SpaceService spaceService;

    /**
     * 分页查询
     * @param space
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page")
    public Result<Page<Space>> queryByPage(@RequestBody Space space, int pageNum, int pageSize) {
        return Result.success(this.spaceService.queryByPage(space, pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/search/{id}")
    public Result<Space> queryById(@PathVariable("id") Integer id) {

        return Result.success(this.spaceService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param space 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Space> add(@RequestBody Space space) {
        return Result.success(this.spaceService.insert(space));
    }

    /**
     * 编辑数据
     *
     * @param space 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    public Result<Space> edit(@RequestBody Space space) {
        return Result.success(this.spaceService.update(space));
    }

    /**
     * 删除数据（逻辑删除）
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public Result<Boolean> deleteById(Integer id) {
        return Result.success(this.spaceService.deleteById(id));
    }


    /**
     * 获取所有车位类型
     *
     * @return
     */
    @GetMapping("/place-type")
    public Result<List<PlaceTypeVo>> getAllPlaceType(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            log.info("请求头：{}", name);
        }
        log.info("请求用户id：{}", request.getHeader("userId"));
        return Result.success(this.spaceService.getAllPlaceType());
    }

    /**
     * 根据车位类型获取车位信息
     *
     * @param type
     * @return
     */
    @GetMapping("/place-type/{type}")
    public Result<List<Space>> getPlaceByType(@PathVariable("type") String type) {

        return Result.success(this.spaceService.getPlaceByType(type));
    }

    /**
     * 设置车位状态
     * @param spaceStatusDTO
     * @return
     */
    @PutMapping("/setStatus")
    public Result<Boolean> setStatus(@RequestBody SpaceStatusDTO spaceStatusDTO) {
        return Result.success(this.spaceService.setStatus(spaceStatusDTO));
    }
}

